<?php


namespace App\Services;

use Illuminate\Http\Request as HttpRequest;
use Illuminate\Http\Response as HttpResponse;

class Response
{

    public static function apiSuccess($data, $msg='success'):HttpResponse{
        $content = [
            'code' => 200,
            'msg' => $msg,
            'data' => $data
        ];
        return new HttpResponse($content);
    }

    public static function setNotCORS(HttpRequest $request){
        header('Access-Control-Allow-Origin:*'); // *代表允许任何网址请求
        header('Content-Type: text/html;charset=utf-8');
        if($request->getMethod()==HttpRequest::METHOD_OPTIONS){
            header('Access-Control-Allow-Headers: Authorization, Content-Type');
            header('Access-Control-Allow-Methods: GET, POST, PUT, PATCH, DELETE, HEAD, OPTIONS');
//            header('Access-Control-Max-Age: 86400');//允许86400秒[24小时]缓存
            header('Access-Control-Request-Method: GET,POST');
            header('Authorization: *');
            exit();
        }
        header('Access-Control-Allow-Methods:POST,GET,OPTIONS,DELETE'); // 允许请求的类型
        header('Access-Control-Allow-Headers: Content-Type,Content-Length,Accept-Encoding,X-Requested-with, Origin'); // 设置允许自定义请求头的字段
    }

}
